package com.world.cat.service.sys;

import com.world.cat.dao.sys.UserAreaDao;
import com.world.cat.model.sys.UserArea;
import com.world.common.util.SysUtil;
import com.world.core.dao.GenericRepository;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
@Transactional
public class UserAreaService extends BaseService<UserArea> {
    @Resource
    private UserAreaDao userAreadao;

    @Override
    public GenericRepository getDao() {
        return userAreadao;
    }
    

    public void saveUserArea(String userId, JSONArray jsonArray) {
        List<UserArea> userAreas = userAreadao.findByUserId(userId);
        userAreadao.delete(userAreas);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            UserArea userArea = new UserArea();
            userArea.setUserId(userId);
            userArea.setAreaId(SysUtil.valueOf(jsonObject.get("areaId")));
            userAreadao.save(userArea);
        }
    }
}
